//
//  RBGoodsDetailWebView.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/29.
//

#import "RBGoodsDetailWebView.h"
#import <WebKit/WebKit.h>
#import <Masonry.h>

@interface RBGoodsDetailWebView ()<WKNavigationDelegate, WKNavigationDelegate>

@property(nonatomic, strong) WKWebView *webview;
@property(nonatomic, assign) CGFloat currentHeight;

@property(nonatomic, assign) NSInteger counter;

@end

@implementation RBGoodsDetailWebView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.userInteractionEnabled = NO;
        self.currentHeight = 10;
    }
    return self;
}

- (void)dealloc
{
    [_webview.scrollView removeObserver:self forKeyPath:@"contentSize"];
    [_webview removeObserver:self forKeyPath:@"estimatedProgress"];
}

#pragma mark - Event

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"])
    {
        if (ABS(self.webview.scrollView.contentSize.width-self.frame.size.width)  > self.frame.size.width*0.1)
        {
            return;
        }
        CGFloat height = MAX(self.webview.scrollView.contentSize.height, 10);
        if (ABS(self.currentHeight - height) > 3)
        {
            self.currentHeight = height;
            if (self.heightChangeBlock)
            {
                self.heightChangeBlock(self.currentHeight);
            }
            if(self.autoUpdateHeight)
            {
                [self mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(self.currentHeight);
                }];
            }
        }
    }
    else if([keyPath isEqualToString:@"estimatedProgress"])
    {
        if (self.webview.estimatedProgress > 0.1)
        {
//            NSLog(@"进度:%.1f",self.webview.estimatedProgress);
        }
    }
}

#pragma mark - Setter

- (void)setUrl:(NSString *)url
{
    _url = url;
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}

- (void)setContent:(NSString *)content
{
    _content = content;
    NSString *newHtmlString = [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><script>function show(s){document.location = '%@'+s;}</script><style type='text/css'>img{width:100%%}</style>%@", @"rb_pic_jump", content];
    [self.webview loadHTMLString:newHtmlString baseURL:nil];
}

#pragma mark - Getter

- (WKWebView *)webview
{
    if (!_webview)
    {
        self.webview = ({
            WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
            WKWebView *view = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
            view.scrollView.scrollEnabled = NO;
            [view.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
            [view addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];

            view.backgroundColor = UIColor.whiteColor;
            view.allowsBackForwardNavigationGestures = NO;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(self);
                make.top.left.mas_equalTo(0);
                make.right.bottom.mas_equalTo(0);
            }];
            view;
        });
    }
    return _webview;
}

@end
